//
//  ANWebService+ShoppingCar.m
//  Ants
//
//  Created by ZHM on 16/8/30.
//  Copyright © 2016年 ZHM. All rights reserved.
//

#import "ANWebService+ShoppingCar.h"

@implementation ANWebService (ShoppingCar)

- (void)fetchCurrentUserShoppingItemsCompletion:(void(^)(NSArray *shoppingItems, NSError *error))completion {
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"ShoppingItems"];
    ANBmobUser *user = [self currentUser];
    [bquery whereObjectKey:@"shoppingItems" relatedTo:user];
    [bquery includeKey:@"product"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        if (!error) {
            NSMutableArray *arrayM = [NSMutableArray array];
            if (array.count) {
                [array enumerateObjectsUsingBlock:^(BmobObject *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    
                    ANBDShoppingItem *item = [sharedWebService shoppingItemFromBmobObject:obj];
                    [arrayM addObject:item];
                }];
            }
            BLOCK_SAFE_ASY_RUN_MainQueue(completion, arrayM, nil);
        } else {
            BLOCK_SAFE_ASY_RUN_MainQueue(completion, nil, error);
        }
    }];
}

- (void)addShoppingItemWithProduct:(ANBDProduct *)product Completion:(void(^)(BOOL succeeded, NSError *error))completion {
    ANBmobUser *user = [self currentUser];
    //验证当前用户sessionToken 有效性
    [self updateNickName:user.nickName completion:^(BOOL succeeded, NSError *error) {
        if (!error) {
            BmobQuery *bquery = [BmobQuery queryWithClassName:@"ShoppingItems"];
            [bquery whereObjectKey:@"shoppingItems" relatedTo:user];
            [bquery whereKey:@"product" equalTo:product.bmObject];
            [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
                if (!error) {
                    if (array.count == 0) {
                        ANLog(@"添加");
                        BmobObject  *object = [BmobObject objectWithClassName:@"ShoppingItems"];
                        [object setObject:product.bmObject forKey:@"product"];
                        if (product.quantity == 0) {
                            [object setObject:@0 forKey:@"quantity"];
                        } else {
                            [object setObject:@1 forKey:@"quantity"];
                        }
                        [object saveInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                            if (!error) {
                                BmobRelation *relation = [[BmobRelation alloc] init];
                                [relation addObject:object];
                                [user addRelation:relation forKey:@"shoppingItems"];
                                [user updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                                    if (!error) {
                                        ANLog(@"加入购物车");
                                        BLOCK_SAFE_ASY_RUN_MainQueue(completion,YES,nil);
                                    } else {
                                        ANLog(@"加入购物车错误:%@",error);
                                        BLOCK_SAFE_ASY_RUN_MainQueue(completion,NO,error);
                                    }
                                }];
                            } else {
                                ANLog(@"添加错误:%@",error);
                                BLOCK_SAFE_ASY_RUN_MainQueue(completion,NO,error);
                            }
                        }];
                    } else {
                        //                ANLog(@"购物车里已经存在 商品数量+1 --");
                        //                BmobObject *oneObject = array[0];
                        //                NSUInteger quantity = [[oneObject objectForKey:@"quantity"] integerValue];
                        //                ANLog(@"quantity = %ld",quantity);
                        //                [oneObject setObject:[NSNumber numberWithUnsignedInteger:++quantity] forKey:@"quantity"];
                        //                [oneObject updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                        //                    if (!error) {
                        //                        ANLog(@"数量加1");
                        BLOCK_SAFE_ASY_RUN_MainQueue(completion,YES,nil);
                        //                    } else {
                        //                        ANLog(@"数量加1错误,%@",error);
                        //                        BLOCK_SAFE_ASY_RUN_MainQueue(completion,NO,error);
                        //                    }
                        //                }];
                    }
                } else {
                    ANLog(@"查找错误:%@",error);
                    BLOCK_SAFE_ASY_RUN_MainQueue(completion,NO,error);
                }
            }];
        } else {
            BLOCK_SAFE_ASY_RUN_MainQueue(completion,NO,error);
        }
    }];
    
}
- (void)updateShoppingItem:(ANBDShoppingItem *)shoppingItem Quantity:(NSUInteger)quantity  completion:(void (^)(BOOL succeeded, NSError *error))completion {
    BmobQuery *bquery = [BmobQuery queryWithClassName:@"ShoppingItems"];
    [bquery getObjectInBackgroundWithId:shoppingItem.mlObjectId block:^(BmobObject *object, NSError *error) {
        if (!error) {
            [object setObject:[NSNumber numberWithInteger:quantity] forKey:@"quantity"];
            [object updateInBackgroundWithResultBlock:^(BOOL isSuccessful, NSError *error) {
                if (!error) {
                    ANLog(@"数量加1");
                    BLOCK_SAFE_ASY_RUN_MainQueue(completion,YES,nil);
                } else {
                    ANLog(@"数量加1错误,%@",error);
                    BLOCK_SAFE_ASY_RUN_MainQueue(completion,NO,error);
                }
            }];
        } else {
            ANLog(@"查找错误,%@",error);
            BLOCK_SAFE_ASY_RUN_MainQueue(completion,NO,error);
        }
    }];
}

- (void)removeShoppingItem:(ANBDShoppingItem *)shoppingItem  completion:(void(^)(BOOL succeeded, NSError *error))completion {
    BmobObject *bmobObject = [BmobObject objectWithoutDataWithClassName:@"ShoppingItems"  objectId:shoppingItem.mlObjectId];
    [bmobObject deleteInBackgroundWithBlock:^(BOOL isSuccessful, NSError *error) {
        if (!error) {
            ANLog(@" mlObjectId---%@",shoppingItem.mlObjectId);

            BLOCK_SAFE_ASY_RUN_MainQueue(completion,YES,nil);
            [SVProgressHUD showSuccessWithStatus:@"删除成功!"];
        }else {
            [SVProgressHUD showSuccessWithStatus:@"删除失败!"];
            BLOCK_SAFE_ASY_RUN_MainQueue(completion,NO,error);
        }
    }];
}

@end
